import { reqDetailList ,reqAddCartSuccess } from '@/api'

const actions = {
    // detail数据
    async getDetailList({ commit, dispatch, state, getters }, skyId) {
        let result = await reqDetailList(skyId)
        commit('GETDETAILLIST', result.data)
    },
    //加入购物车接口
    async getCartSuccess({ commit, dispatch, state, getters }, {skyNum, skyId}){
        let result =  await reqAddCartSuccess(skyId,skyNum)
        if(result.code == '200'){
            return 'ok'
        }else{
            return Promise.reject(new Error('请求失败'))
        }
    }
}
const state = {
    detailList: {}
}
const getters = {
    //面包屑计算属性
    categoryView(state) {
        // 第一次dispath获取的是空对象,undefined 点属性会报错
        return state.detailList.categoryView || {}
    },
    //销售属性
    spuSaleAttrList(state) {
        return state.detailList.spuSaleAttrList || []
    },
    // 右侧商品数据展示
    skuInfo(state) {
        return state.detailList.skuInfo || {}
    },
    //轮播图
    skuImageList(state) {
        return (state.detailList.skuInfo || {}).skuImageList || []
    }
}
const mutations = {
    GETDETAILLIST(state, payload) { state.detailList = payload }
}

export default {
    actions,
    state,
    getters,
    mutations
}